//
//  YYPhotoModel.swift
//  CodeLibrary_Swift
//
//  Created by 陆文宇 on 2017/2/8.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit
import Photos

class YYPhotoModel: CustomStringConvertible {
    
    /// 照片压缩图
    let image: UIImage
    /// 照片资源
    let asset: PHAsset!
    /// 照片原图
    var originalImage: UIImage {
        guard asset != nil else {
            return image
        }
        
        let assets: PHFetchResult<PHAsset> = PHAsset.fetchAssets(withLocalIdentifiers: [asset.localIdentifier], options: nil)
        if assets.count == 0 {
            return image
        }
        
        let options: PHImageRequestOptions = PHImageRequestOptions()
        options.isSynchronous = true
        
        let size: CGSize = CGSize(width: assets.firstObject!.pixelWidth, height: assets.firstObject!.pixelHeight)
        
        var originalImage = image
        PHImageManager.default().requestImage(for: assets.firstObject!, targetSize: size, contentMode: .default, options: options) { (result, info) in
            if let image = result {
                originalImage = image
            }
        }
        
        return originalImage
    }
    var chooseNumber: Int = 0
    init(image: UIImage, asset: PHAsset) {
        self.image = image
        self.asset = asset
    }
    
    /// 模型描述
    var description: String {
        return "image: " + "\(image)" + "asset" + "\(asset)" + "originalImage" + "\(originalImage)" + "chooseNumber" + "\(chooseNumber)"
    }
    
}
